import java.nio.ByteBuffer;

public class BufferTest {

    public static void main(String[] args) {

        ByteBuffer byteBuffer = ByteBuffer.allocate(48);
//        ByteBuffer byteBuffer = ByteBuffer.allocateDirect(48);//直接缓冲区  --->  用户态、内核态内存空间不进行数据传输，直接映射（指向）一个内存块。
//        byteBuffer.isDirect(); //可用来判断 是否为 直接缓存区

        System.out.println("初始阶段-limit->:" + byteBuffer.limit());
        System.out.println("初始阶段-position->:" + byteBuffer.position());
        System.out.println("初始阶段-capacity->:" + byteBuffer.capacity());
        System.out.println("初始阶段-mark->:" + byteBuffer.mark());

        System.out.println("--------------------------");

//        byteBuffer.put(new byte[]{'c', 'a', 'o', 'c', 'a', 'o'});//一个字节  占1位 ===>  limit = 6
        byteBuffer.put("你好哦".getBytes());//一个中文 占3位 ==> limit = 9

        System.out.println("put 数据后-limit->:" + byteBuffer.limit());
        System.out.println("put 数据后-position->:" + byteBuffer.position());
        System.out.println("put 数据后-capacity->:" + byteBuffer.capacity());
        System.out.println("put 数据后-mark->:" + byteBuffer.mark());

        System.out.println("--------------------------");

        byteBuffer.flip();

        System.out.println("flip 之后-limit->:" + byteBuffer.limit());
        System.out.println("flip 之后-position->:" + byteBuffer.position());
        System.out.println("flip 之后-capacity->:" + byteBuffer.capacity());
        System.out.println("flip 之后-mark->:" + byteBuffer.mark());

        System.out.println("--------------------------");
        byte b = byteBuffer.get();
        System.out.println("b==>" + b);


//        byteBuffer.put("再加两个".getBytes());

        System.out.println("get 之后-limit->:" + byteBuffer.limit());
        System.out.println("get 之后-position->:" + byteBuffer.position());
        System.out.println("get 之后-capacity->:" + byteBuffer.capacity());
        System.out.println("get 之后-mark->:" + byteBuffer.mark());
//
//        System.out.println("--------------------------");

//        System.out.println("内容输出：" + new String(byteBuffer.array()));
    }

}
